﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.XPath;

namespace workshop3
{
    public partial class Formsearch : Form
    {

        string courseid;
        string studentidwtf;
        string year;
        string semester;

        List<string> yearlist = new List<string>();
        List<string> semelist = new List<string>();

        DataTable grade = new DataTable();
        DataColumn name = new DataColumn("Name");
        DataColumn id = new DataColumn("ID");
        DataColumn finalgrade = new DataColumn("Mark");


        public Formsearch()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            grade.Columns.Add(id);
            grade.Columns.Add(name);
            grade.Columns.Add(finalgrade);
            cbyear.SelectedValueChanged -= cbyear_SelectedValueChanged;
            cbsemester.SelectedValueChanged -= cbsemester_SelectedValueChanged;
            cbcourse.SelectedValueChanged -= cbcourse_SelectedValueChanged;

            if (File.Exists("rubric.xml") && File.Exists("course.xml"))
            {//load all the year and semester information
                XmlDocument coursedoc = new XmlDocument();
                coursedoc.Load("course.xml");
                string coursepath = "//course";
                XmlNodeList coursenodelist = coursedoc.SelectNodes(coursepath);

                if (coursenodelist != null)
                {
                    for (int i = 0; i < coursenodelist.Count; i++)
                    {
                        if (yearlist.Count == 0 && semelist.Count == 0)
                        {
                            //courselist.Rows.Add(coursenodelist[i].Attributes[0].Value.ToString(),coursenodelist[i].ChildNodes[0].InnerText);
                            yearlist.Add(coursenodelist[i].ChildNodes[1].InnerText);
                            semelist.Add(coursenodelist[i].ChildNodes[2].InnerText);
                        }
                        else
                        {
                            if (yearlist.Contains(coursenodelist[i].ChildNodes[1].InnerText))
                            {
                                //continue;
                            }
                            else
                            {
                                yearlist.Add(coursenodelist[i].ChildNodes[1].InnerText);
                            }
                            if (semelist.Contains(coursenodelist[i].ChildNodes[2].InnerText))
                            {
                                //continue;
                            }
                            else
                            {
                                semelist.Add(coursenodelist[i].ChildNodes[2].InnerText);
                            }
                        }
                    }

                    cbyear.DataSource = yearlist;
                    cbsemester.DataSource = semelist;
                    cbyear.SelectedValueChanged += cbyear_SelectedValueChanged;
                    cbsemester.SelectedValueChanged += cbsemester_SelectedValueChanged;
                    //cbcourse.SelectedValueChanged += cbcourse_SelectedValueChanged;
                }
            }





        }

        private void cbyear_SelectedValueChanged(object sender, EventArgs e)
        {//change the dataset of course droup-down box
            cbcourse.DataSource = null;
            ChangeCourse(cbyear.SelectedValue.ToString(), cbsemester.SelectedValue.ToString());
            //DataTable dt0 = new DataTable();
            //DataColumn dc0 = new DataColumn();
            //DataColumn dc1 = new DataColumn();
            //dt0.Columns.Add(dc0);
            //dt0.Columns.Add(dc1);

            //cbcourse.DataSource = null;
            //year = cbyear.SelectedValue.ToString();
            //if (year != null && semester != null) {
            //    if (File.Exists("course.xml")) {
            //        XmlDocument coursedoc = new XmlDocument();
            //        coursedoc.Load("course.xml");
            //        string coursepath = "//course";
            //        XmlNodeList coursenodelist = coursedoc.SelectNodes(coursepath);
            //        if (coursenodelist.Count != 0)
            //        {
            //            foreach (XmlNode xn in coursenodelist)
            //            {
            //                if (year == xn.ChildNodes[1].InnerText && xn.ChildNodes[2].InnerText == semester)
            //                {
            //                    //courselist.Add(xn.ChildNodes[0].InnerText);
            //                    dt0.Rows.Add(xn.ChildNodes[0].InnerText, xn.Attributes[0].InnerText);
            //                }
            //            }
            //        }
            //        cbcourse.DataSource = dt0;
            //        cbcourse.DisplayMember = dt0.Columns[0].ToString();
            //        cbcourse.ValueMember = dt0.Columns[1].ToString();


            //    }





            //}

        }

        private void cbsemester_SelectedValueChanged(object sender, EventArgs e)
        {//change the dataset of the course drop-down box
            cbcourse.DataSource = null;
            ChangeCourse(cbyear.SelectedValue.ToString(), cbsemester.SelectedValue.ToString());
            //DataTable dt0 = new DataTable();
            //DataColumn dc0 = new DataColumn();
            //DataColumn dc1 = new DataColumn();
            //dt0.Columns.Add(dc0);
            //dt0.Columns.Add(dc1);

            //cbcourse.DataSource = null;
            //semester = cbsemester.SelectedValue.ToString();
            //if (year != null && semester != null)
            //{
            //    if (File.Exists("course.xml"))
            //    {
            //        XmlDocument coursedoc = new XmlDocument();
            //        coursedoc.Load("course.xml");
            //        string coursepath = "//course";
            //        XmlNodeList coursenodelist = coursedoc.SelectNodes(coursepath);
            //        if (coursenodelist.Count != 0)
            //        {
            //            foreach (XmlNode xn in coursenodelist)
            //            {
            //                if (year == xn.ChildNodes[1].InnerText && xn.ChildNodes[2].InnerText == semester)
            //                {
            //                    //courselist.Add(xn.ChildNodes[0].InnerText);
            //                    dt0.Rows.Add(xn.ChildNodes[0].InnerText, xn.Attributes[0].InnerText);
            //                }
            //            }
            //        }
            //        cbcourse.DataSource = dt0;
            //        cbcourse.DisplayMember = dt0.Columns[0].ToString();
            //        cbcourse.ValueMember = dt0.Columns[1].ToString();


            //    }





            //}
        }

        private void cbcourse_SelectedValueChanged(object sender, EventArgs e)
        {//change the datagridview dataset based on the course info
            if (cbcourse.DataSource != null)
            {
                Showinfor(cbcourse.SelectedValue.ToString());
            }
            //MessageBox.Show()
        }

        private string getname(string courseid, string studentid, string type)
        {// this function is to get the student name from student xml by student id
            if (type == "studentname")
            {
                XmlNodeList course = ClassCreateXML.loadxmldata(courseid, "//course", "course.xml", "studentfile");
                //XmlNode studentfile = course[7];
                string filename = course[course.Count-1].InnerText.ToString() + ".xml";
                XmlDocument doc = new XmlDocument();
                doc.Load(filename);
                XmlNodeList namelist = doc.SelectNodes("//student");
                for (int i = 0; i < namelist.Count; i++)
                {
                    if (namelist[i].ChildNodes[0].InnerText.ToString() == studentid)
                    {
                        return namelist[i].ChildNodes[1].InnerText.ToString();
                    }
                }

            }
            else if (type == "taskname")
            {
                XmlNodeList course = ClassCreateXML.loadxmldata(courseid, "//course", "course.xml", "task");
                for (int i = 4; i < course.Count - 1; i++)
                {
                    if (course[i].Attributes[0].Value.ToString() == studentid)
                    {
                        return course[i].InnerText.ToString();
                    }
                }

                return "Final mark";

            }
            return "unknown";
        }

        private void ChangeCourse(string year, string semester)
        {//this function is to change the dataset of course drop-down box
            bool find = false;
            DataTable dt0 = new DataTable();
            DataColumn dc3 = new DataColumn("coursename");
            DataColumn dc4 = new DataColumn("courseid");
            dt0.Columns.Add(dc4);
            dt0.Columns.Add(dc3);
            if (File.Exists("course.xml"))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("course.xml");
                string path = "//course";
                XmlNodeList nodelist = doc.SelectNodes(path);
                if (nodelist.Count != 0)
                {
                    for (int i = 0; i < nodelist.Count; i++)
                    {
                        if (nodelist[i].ChildNodes[1].InnerText == year && nodelist[i].ChildNodes[2].InnerText == semester)
                        {
                            find = true;
                            dt0.Rows.Add(nodelist[i].Attributes[0].Value, nodelist[i].ChildNodes[0].InnerText);
                        }

                    }
                    if (find)
                    {
                        cbcourse.DataSource = dt0;
                        cbcourse.DisplayMember = dt0.Columns[1].ToString();
                        cbcourse.ValueMember = dt0.Columns[0].ToString();
                        cbcourse.SelectedValueChanged += cbcourse_SelectedValueChanged;
                    }


                }
                else
                {
                    MessageBox.Show("Can not find any course!");
                }

                //return dt0;

            }
            //return null;
        }

        private void Showinfor(string courseid)
        {// this function is for load and show the dataset of datagridview
            bool find = false;
            string studentcheck;
            string studentname;
            string str1 = "";
            double[] store = new double[30];
            grade.Clear();
            if (File.Exists("grade.xml")&&courseid!=null)
            {
                XmlDocument coudoc = new XmlDocument();
                coudoc.Load("grade.xml");
                string rubpath = "//Grades/grade[@courseid="+courseid+"]";
                XmlNode node = coudoc.SelectSingleNode(rubpath);
                if (node.ChildNodes.Count != 0)
                {
                    //MessageBox.Show("fuck1");
                    //for (int i = 0; i < node.ChildNodes.Count-1; i++)
                    //{
                    //    DataColumn dc = new DataColumn(node.ChildNodes[i].Attributes[0].Value);
                    //    grade.Columns.Add(dc);
                    //    //if (rubnodelist[i].Attributes[0].Value == rubricid)
                    //    //{
                    //    //    find = true;
                    //    //    lblname.Text = rubnodelist[i].Attributes[1].Value;
                    //    //    XmlNodeList item = rubnodelist[i].ChildNodes;
                    //    //    foreach (XmlNode xn in item)
                    //    //    {
                    //    //        rubric.Rows.Add(xn.Attributes[0].Value.ToString(), xn.Attributes[1].Value.ToString(), xn.InnerText);
                    //    //    }
                    //    //}
                    //}

                    for (int i = 0; i < node.ChildNodes[0].ChildNodes.Count; i++)
                    {
                        studentcheck = node.ChildNodes[0].ChildNodes[i].Attributes[0].InnerText;
                        studentname = getname(courseid, studentcheck, "studentname");
                        for (int k = 0; k < node.ChildNodes.Count; k++)
                        {
                            for (int m = 0; m < node.ChildNodes[0].ChildNodes.Count; m++)
                            {
                                if (node.ChildNodes[k].ChildNodes[m].Attributes[0].InnerText == studentcheck)
                                {
                                    double.TryParse(node.ChildNodes[k].ChildNodes[m].InnerText, out store[k]);
                                }

                            }


                        }
                        for (int j = 0; j < node.ChildNodes.Count; j++)
                            str1 = str1 + "task" + j.ToString() + ":" + store[j].ToString();

                        grade.Rows.Add(studentcheck,studentname,str1);

                        str1 = "";

                    }

                    //if (find)
                    //{
                     dgvgrade.DataSource = grade;
                      //dgvgrade.Columns[0].Visible = false;
                    //}
                }
                else
                {
                    dgvgrade.DataSource = null;
                }

            }
        }

        private void btn_user_Click(object sender, EventArgs e)
        {//go bcak to management page
            Formmanagement user = new Formmanagement();
            user.Show();
            this.Hide();
        }

        private void cbcourse_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
